package com.tc168.cp306.b06.iview;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class ChannelUtil {

    /**
     * 获取渠道名
     * @param context 此处习惯性的设置为activity，实际上context就可以
     * @return 如果没有获取成功，那么返回值为空
     */
    public static String getChannelName(Context context) {
        if (context == null) {
            return null;
        }
        String channelName = null;
        try {
            synchronized(context){
                PackageManager packageManager = context.getPackageManager();
                if (packageManager != null) {
                    //注意此处为ApplicationInfo 而不是 ActivityInfo,因为渠道设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo



                    ApplicationInfo applicationInfo = packageManager.
                            getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
                    if (applicationInfo != null) {
                        if (applicationInfo.metaData != null) {
                            channelName = String.valueOf(applicationInfo.metaData.get("UMENG_CHANNEL"));
                        }
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return channelName;
    }
}
